# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=webkitgtk
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}2" "${MINGW_PACKAGE_PREFIX}-${_realname}3")
pkgver=2.4.11
pkgrel=5
pkgdesc="GTK+ Web content engine library (mingw-w64)"
arch=('any')
url="https://webkitgtk.org/"
license=("custom")
options=('!libtool' 'strip' '!debug' 'staticlibs' 'buildflags')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-gtk2"
             "${MINGW_PACKAGE_PREFIX}-gtk3"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-ruby"
             "bison"
             "flex"
             "gperf"
             "libtool"
             "gtk-doc"
             "perl"
             "python2")
depends=("${MINGW_PACKAGE_PREFIX}-angleproject"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-enchant"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
         "${MINGW_PACKAGE_PREFIX}-gstreamer"
         "${MINGW_PACKAGE_PREFIX}-geoclue"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libidn"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsoup"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-xz")
source=(https://webkitgtk.org/releases/webkitgtk-${pkgver}.tar.xz
        0020-W32-Fixup-dummy-HeapStatistics-implementation.all.patch
        0021-W32-Change-printf-format-atribute-to-gnu_printf-.all.patch
        0022-gl-casts.patch
        0101-webkitgtk-2.4.3-gcc-asm.all.patch
        0102-fix-build-with-icu-59.1.patch
        webkitgtk-2.4.9-abs.patch)

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/0020-W32-Fixup-dummy-HeapStatistics-implementation.all.patch
  patch -p1 -i ${srcdir}/0021-W32-Change-printf-format-atribute-to-gnu_printf-.all.patch
  patch -p1 -i ${srcdir}/0022-gl-casts.patch
  patch -p1 -i ${srcdir}/0101-webkitgtk-2.4.3-gcc-asm.all.patch
  patch -p1 -i ${srcdir}/0102-fix-build-with-icu-59.1.patch
  # borrowed from Arch Linux
  patch -p1 -i ${srcdir}/webkitgtk-2.4.9-abs.patch

  # autoreconf --verbose --install -I Source/autotools

  rm -rf ${srcdir}/build-gtk{2,3}
  mkdir -p ${srcdir}/build-gtk{2,3}
}

_build() {
  _ver="$1"; shift
  cd ${srcdir}/build-${_ver}

  export PYTHON=/usr/bin/python2
  export RUBY=${MINGW_PREFIX}/bin/ruby

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-debug \
    --enable-introspection \
    --disable-credential-storage \
    --disable-accelerated-compositing \
    --enable-debug-symbols=min \
    --disable-webkit2 \
    --disable-glibtest \
    --disable-static \
    --enable-shared \
    --enable-video \
    --enable-spellcheck \
    --enable-optimizations \
    --enable-geolocation \
    --disable-x11-target \
    --enable-win32-target \
    --enable-gles2=no \
    --enable-egl=no \
    "$@"

  make -j1
}

build() {
  local _jit_flag=
  case ${MINGW_CHOST} in
    i686*)
      _jit_flag="-enable-jit"
    ;;
    x86_64*)
      _jit_flag="-disable-jit"
    ;;
  esac

  _build gtk3 --disable-gtk-doc --disable-webkit2 --with-gtk=3.0 ${_jit_flag}
  _build gtk2 --disable-gtk-doc --disable-webkit2 --with-gtk=2.0 ${_jit_flag}
}

package_webkitgtk2() {
  pkgdesc+=" for GTK2"
  depends+=(${MINGW_PACKAGE_PREFIX}-gtk2)
  #provides=("${MINGW_PACKAGE_PREFIX}-libwebkit2=${pkgver}")
  cd "${srcdir}/build-gtk2"
  make -j1 DESTDIR="${pkgdir}" install
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
}

package_webkitgtk3() {
  depends+=(${MINGW_PACKAGE_PREFIX}-gtk3)
  optdepends+=("${MINGW_PACKAGE_PREFIX}-gtk2: Netscape plugin support")
  #provides=("${MINGW_PACKAGE_PREFIX}-webkitgtk3=${pkgver}" "${MINGW_PACKAGE_PREFIX}-libwebkit3=${pkgver}")
  cd "${srcdir}/build-gtk3"
  make -j1 DESTDIR="${pkgdir}" install
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
}

package_mingw-w64-i686-webkitgtk2() {
  package_webkitgtk2
}

package_mingw-w64-i686-webkitgtk3() {
  package_webkitgtk3
}

package_mingw-w64-x86_64-webkitgtk2() {
  package_webkitgtk2
}

package_mingw-w64-x86_64-webkitgtk3() {
  package_webkitgtk3
}

sha256sums=('588aea051bfbacced27fdfe0335a957dca839ebe36aa548df39c7bbafdb65bf7'
            'e47cf0649f4bc82f96b6f3830964eeb685c56bdfabe43d83a45cfb432df1a261'
            '6937cbfee2b689084c40785288bedd7aa1f0991c7f922203d2b89a4f67b75836'
            '2a74b1174410121fe8a25b1a87dfeadd22aebd0337df8a31a42ec99ee1585389'
            '82f089e1271ccd39ea3fee7c066a06eabb287d6273910c9e9bddb86ba5ad2818'
            '3d17c925cd6b21a2aafb4d91913b0b3e1a11f532e31190161398b228c367bd31'
            'ec294bbb5588a1802a68e3615c6718486b22f922645c5fef686d3d103014bf70')
